package com.jprocms.module.cms.service.task;

import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.task.vo.TaskCreateReqVO;
import com.jprocms.module.cms.controller.admin.task.vo.TaskExportReqVO;
import com.jprocms.module.cms.controller.admin.task.vo.TaskPageReqVO;
import com.jprocms.module.cms.controller.admin.task.vo.TaskUpdateReqVO;
import com.jprocms.module.cms.convert.task.TaskConvert;
import com.jprocms.module.cms.dal.dataobject.task.TaskDO;
import com.jprocms.module.cms.dal.mysql.task.TaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.TASK_NOT_EXISTS;

/**
 * 任务 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskMapper taskMapper;

    @Override
    public Long createTask(TaskDO taskDO) {
        taskMapper.insert(taskDO);
        // 返回
        return taskDO.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createTask(TaskCreateReqVO createReqVO) {
        // 插入
        TaskDO task = TaskConvert.INSTANCE.convert(createReqVO);
        taskMapper.insert(task);
        // 返回
        return task.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTask(TaskUpdateReqVO updateReqVO) {
        // 校验存在
        validateTaskExists(updateReqVO.getId());
        // 更新
        TaskDO updateObj = TaskConvert.INSTANCE.convert(updateReqVO);
        taskMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTask(TaskDO taskDO) {
        // 校验存在
        validateTaskExists(taskDO.getId());
        taskMapper.updateById(taskDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTask(Long id) {
        // 校验存在
        validateTaskExists(id);
        // 删除
        taskMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteTask(Collection<Long> ids) {
        for (Long id : ids) {
            deleteTask(id);
        }
    }

    private void validateTaskExists(Long id) {
        if (taskMapper.selectById(id) == null) {
            throw exception(TASK_NOT_EXISTS);
        }
    }

    @Override
    public TaskDO getTask(Long id) {
        return taskMapper.selectById(id);
    }

    @Override
    public List<TaskDO> getTaskList(Collection<Long> ids) {
        return taskMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TaskDO> getTaskPage(TaskPageReqVO pageReqVO) {
        return taskMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TaskDO> getTaskList(TaskExportReqVO exportReqVO) {
        return taskMapper.selectList(exportReqVO);
    }

}
